package org.luo.user.domain.user;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;

import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Pattern;

import org.hibernate.validator.constraints.URL;
import org.luo.common.common.Result;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;



    @PostMapping("/register")
    @Operation(summary = "注册", description = "注册")
    public Result register(
            @Pattern(regexp = "^\\S{5,16}$")
            @Parameter(description="用户名")
            @RequestParam("username")
            String username,
            @Pattern(regexp = "^\\S{5,16}$")
            @Parameter(description="密码")
            @RequestParam("password")
            String password){

        return userService.register(username, password);
    }

    @PostMapping("/login")
    @Operation(summary = "登录", description = "登录")
    public Result login(
            @Pattern(regexp = "^\\S{5,16}$")
            @Parameter(description="用户名")
            @RequestParam("username")
            String username,
            @Pattern(regexp = "^\\S{5,16}$")
            @Parameter(description="密码")
            @RequestParam("password")
            String password){

        return userService.login(username, password);
    }

    @GetMapping("/list")
    @Operation(summary = "用户列表", description = "用户列表")
    public Result<Page<User>> list(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        return userService.getList(pageNum, pageSize);
    }

    @GetMapping("/userInfo")
    @Operation(summary = "用户信息", description = "用户信息")
    public Result<User> userInfo(){
        return userService.userInfo();
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户", description = "更新用户")
    public  Result update(@RequestBody @Validated User user){
        return userService.update(user);
    }
    @PutMapping("/updateAvatar")
    @Operation(summary = "更新头像", description = "更新头像")
    public  Result updateAvatar(@RequestParam("avatarUrl") String avatarUrl){
        return userService.updateAvatar(avatarUrl);
    }

    @PutMapping("/updatePwd")
    @Operation(summary = "修改密码", description = "修改密码")
    public  Result updatePwd(
            @RequestParam("oldPwd")
            String oldPwd,
            @RequestParam("newPwd")
            String newPwd,
            @RequestParam("rePwd")
            String rePwd){

        return userService.updatePwd(oldPwd, newPwd, rePwd);
    }
}
